package com.spider.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.spider.constant.YjsUrl;
import com.spider.entity.YjsPriceEntity;

import java.math.BigDecimal;
import java.util.LinkedHashMap;

public class YjsUtil {
    /**
     * 拼接头信息
     * @return
     */
    public static JSONObject getApiinfo(){
        JSONObject apiinfo=new JSONObject(new LinkedHashMap());
        apiinfo.put("sid", YjsUrl.SID);
        long time=System.currentTimeMillis()/1000;
        System.out.println(time);
        apiinfo.put("time",time);
        String sign= StringUtil.md5(""+YjsUrl.SID+time+YjsUrl.appKey);
        apiinfo.put("sign",sign);
        return apiinfo;
    }

    /**
     * 拼接body
     * @return
     */
    public static JSONArray getBody(){
        JSONArray js=new JSONArray();
        JSONObject body=new JSONObject(new LinkedHashMap());
        body.put("品牌","希恩思");
        body.put("货号","M-00980");
        body.put("包装规格","25g");
        body.put("目录价",30);
        body.put("促销价",30);
        js.add(body);
        return js;
    }

    /**
     * 动态拼接body
     * @return
     */
    public static JSONArray getBody(YjsPriceEntity entity){
        JSONArray js=new JSONArray();
        JSONObject body=new JSONObject(new LinkedHashMap());
        if(entity.getPrice()==null){
            return null;
        }
        BigDecimal base=entity.getPrice();
        BigDecimal discount=BigDecimal.ZERO;
        body.put("目录价",base);
        if(entity.getCxbs()!=null&&entity.getCxbs().equals("不打折")){
            discount=base;
        }else{
            //判断产品类型不是氘代的
            if(entity.getType()!=null&&entity.getType().equals("氘代")){
                discount=base;
            }else{
                discount=base.multiply(new BigDecimal(0.85).setScale(2, BigDecimal.ROUND_HALF_UP));
            }
        }
        String gg=StringUtil.substringNum(entity.getNum())+entity.getUnit();
        body.put("促销价",discount);
        body.put("品牌",entity.getBrand());
        body.put("货号",entity.getHs());
        body.put("包装规格",gg);
        js.add(body);
        return js;
    }

    /**
     * 动态库存拼接body
     * @return
     */
    public static JSONArray getStockBody(YjsPriceEntity entity){
        JSONArray js=new JSONArray();
        JSONObject body=new JSONObject(new LinkedHashMap());
        body.put("品牌",entity.getBrand());
        body.put("货号",entity.getHs());
        String gg=StringUtil.substringNum(entity.getNum())+entity.getUnit();
        body.put("包装规格",gg);
        System.out.println("开始转化库存数量");
        body.put("库存数量",Integer.parseInt(StringUtil.substringNum(entity.getNum())));
        body.put("库存详情","天津："+entity.getNumDetail());
        body.put("预计交货期","现货（1-3个工作日）");
        js.add(body);
        return js;
    }


}
